//
//  DFSaleV.swift
//  DailyFresh
//
//  Created by Arthur on 2017/9/20.
//  Copyright © 2017年 DailyFresh. All rights reserved.
//

import UIKit
import SnapKit
import AMScrollingNavbar

class DFPromotionSaleVC: DFViewController, ScrollingNavigationControllerDelegate {
    
    var viewModel: DFHomeVM = DFHomeVM()
    
    override func viewDidLoad() {
        super.viewDidLoad()
        self._setupUI()
        self.viewModel.test()
    }
    
    override func viewDidAppear(_ animated: Bool) {
        super.viewDidAppear(animated)
        if let navigationController = self.navigationController as? ScrollingNavigationController {
//            if let tabBarController = tabBarController {
//                navigationController.followScrollView(collectionView, delay: 0, scrollSpeedFactor: 2, followers: [tabBarController.tabBar])
//            } else {
                navigationController.followScrollView(collectionView, delay: 0.0, scrollSpeedFactor: 1)
//            }
            navigationController.scrollingNavbarDelegate = self
            navigationController.expandOnActive = true
        }
        
    }
    
    lazy var collectionView: UICollectionView = {
        [unowned self] in
        
        let layout = UICollectionViewFlowLayout()

//        let minusHeight = DFLayoutConstants.fullNavigationbarHeight + DFSegmentStyle().titleViewHeight + DFLayoutConstants.tabBarHeight
        var tmpCollectionView = UICollectionView.init(frame: CGRect.zero, collectionViewLayout: layout);
        tmpCollectionView.backgroundColor =  UIColor.white
        tmpCollectionView.dataSource = self
        tmpCollectionView.delegate = self
        tmpCollectionView.register(DFBannerCell.self, forCellWithReuseIdentifier: "DFBannerCell")
        tmpCollectionView.register(DFProductCell.self, forCellWithReuseIdentifier: "DFProductCell")
        tmpCollectionView.register(DFProductGroupTextCell.self, forCellWithReuseIdentifier: "DFProductGroupTextCell")
        tmpCollectionView.register(DFProductGroupImageCell.self, forCellWithReuseIdentifier: "DFProductGroupImageCell")
//        tmpCollectionView.register(DFProductCellHeader.self, forSupplementaryViewOfKind: UICollectionElementKindSectionHeader, withReuseIdentifier: "DFProductCellHeader")
        
        return tmpCollectionView
        }()


}

extension DFPromotionSaleVC {
    func scrollingNavigationController(_ controller: ScrollingNavigationController, didChangeState state: NavigationBarState) {
        switch state {
        case .collapsed:
            print("navbar collapsed")
        case .expanded:
            print("navbar expanded")
        case .scrolling:
            print("navbar is moving")
        }
        self.collectionView.snp.updateConstraints { (make) in
            make.edges.equalTo(self.view).inset(UIEdgeInsetsMake(0, 0, 0, 0))
        }
        
        NotificationCenter.default.post(Notification.init(name: Notification.Name(rawValue: "framechange")))
    }
}

// MARK: - Setup ui
extension DFPromotionSaleVC {
    fileprivate func _setupUI() {
        self.view.addSubview(self.collectionView)
        self.collectionView.snp.makeConstraints { (make) in
            make.edges.equalTo(self.view).inset(UIEdgeInsetsMake(0, 0, 0, 0))
        }
//        self.shyNavBarManager.scrollView = self.collectionView
//        self.shyNavBarManager.extensionView
    }
}

// MARK: - UICollection delegate & data source
extension DFPromotionSaleVC: UICollectionViewDelegate, UICollectionViewDataSource, UICollectionViewDelegateFlowLayout {
    //MARK: CollectionViewDelegate
    func numberOfSections(in collectionView: UICollectionView) -> Int {
        return viewModel.sections()
    }
    
    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return viewModel.rows(section: section)
    }
    
    
    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
        
        var height: CGFloat = 0.0
        let sectionType = viewModel.type(section: indexPath.section)
        switch sectionType {
        case .banner:
            height = DFBannerHeaderViewHeight

        case .products:
            
            let productData = self.viewModel.productArray[indexPath.row]
            
            // 返回商品cell
            if type(of: productData) == DFProductModel.self {
                
                let proudct: DFProductModel = productData as! DFProductModel
                if isArrayEmpty(proudct.product_tags) {
                    height = DFProductCellHeight - normalize(value: 20)
                } else {
                    height = DFProductCellHeight

                }
            }
            
            // 返回group cell
            if type(of: productData) == DFProductGroupModel.self {
                
                let groupData = productData as! DFProductGroupModel
                
                if !isStrEmpty(groupData.name) {
                    
                    //文本
                   height = DFGroupTextCellHeight
                } else {
                    //图片
                    height = DFGroupImageCellHeight
                }
                
                
                
            }
            
        case .none:
            height = 0
        }
    
        return CGSize.init(width: DFLayoutConstants.screenWidth, height: height)
    }
    
    
    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumLineSpacingForSectionAt section: Int) -> CGFloat {
        return normalize(value: 8)
    }
    
    
    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, insetForSectionAt section: Int) -> UIEdgeInsets {
        
        return UIEdgeInsetsMake(0, 0, 0, 0)
    }
    
//    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, referenceSizeForHeaderInSection section: Int) -> CGSize {
//        let type = viewModel.type(section: section)
//        if type == .products {
//            return CGSize.init(width: DFLayoutConstants.screenWidth, height: DFProductCellHeaderHeight)
//        }
//
//        return CGSize.zero
//    }

//    func collectionView(_ collectionView: UICollectionView, viewForSupplementaryElementOfKind kind: String, at indexPath: IndexPath) -> UICollectionReusableView {
//
//        if kind == UICollectionElementKindSectionHeader {
//            let type = viewModel.type(section: indexPath.section)
//            if type == .products {
//                let header = collectionView.dequeueReusableSupplementaryView(ofKind: UICollectionElementKindSectionHeader, withReuseIdentifier: "DFProductCellHeader", for: indexPath)
////                (header as! DFProductCellHeader).update(viewModel: self.viewModel)
//
//                return header
//            }
//            return UICollectionReusableView()
//
//
//        } else {
//            return UICollectionReusableView()
//        }
//    }
    
    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        
        return self._configCell(_:collectionView, cellForItemAt:indexPath)
    }
    
    private func _configCell(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        
        
        let sectionType = viewModel.type(section: indexPath.section)
        switch sectionType {
        case .banner:
           
            let cell:DFBannerCell = collectionView.dequeueReusableCell(withReuseIdentifier: "DFBannerCell", for: indexPath) as! DFBannerCell
            cell.update(viewModel: viewModel)
            return cell
            
   
        case .products:
            let productData = self.viewModel.productArray[indexPath.row]
            
            // 返回商品cell
            if type(of: productData) == DFProductModel.self {
                let cell:DFProductCell = collectionView.dequeueReusableCell(withReuseIdentifier: "DFProductCell", for: indexPath) as! DFProductCell
                //            cell.delegate = self
                cell.updateCell(product: productData as! DFProductModel)
                return cell
            }
            
            // 返回group cell
            if type(of: productData) == DFProductGroupModel.self {
                
                let groupData = productData as! DFProductGroupModel
                
                if !isStrEmpty(groupData.name) {
                    //文本
                    
                    let cell:DFProductGroupTextCell = collectionView.dequeueReusableCell(withReuseIdentifier: "DFProductGroupTextCell", for: indexPath) as! DFProductGroupTextCell
                    //            cell.delegate = self
                    cell.update(group: groupData)
                    return cell
                } else {
                    //图片
                    
                    let cell:DFProductGroupImageCell = collectionView.dequeueReusableCell(withReuseIdentifier: "DFProductGroupImageCell", for: indexPath) as! DFProductGroupImageCell
                    //            cell.delegate = self
                    cell.update(group: groupData)
                    return cell
                }
                
          
                
            }
            
            return UICollectionViewCell()

            
        case .none:
             return UICollectionViewCell()
        }
    
    }

    func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
//        let type = self.viewModel.type(section: indexPath.section)
//        if  type == .community {
//            let cell: YYTCmtContentCell = collectionView.cellForItem(at: indexPath) as! YYTCmtContentCell
//            let nextVC = YYTCmtDetailVC.init(dynamicId: Int32((cell.cmt?.dynamicId)!))
//            self.navigationController?.pushViewController(nextVC, animated: true, hideBottomTabBar: true);
//
//        }
        
    }
}


